package org.buptsse.listview;

import java.util.ArrayList;
import java.util.List;

import org.buptsse.entity.CLHotPost;
import org.buptsse.entity.Circle;
import org.buptsse.entity.User;
import org.buptsse.ui.DocListActivity;
import org.buptsse.ui.LogoActivity;
import org.buptsse.ui.MainStudyActivity;
import org.buptsse.ui.R;
//import org.buptsse.ui.TreeActivity;
import org.buptsse.utils.ActionUtils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MyHotListPage extends LinearLayout implements OnScrollListener {
	private final static int OK = 1;
	private final String URL_CIRCLELIST="fetchJoinCircle.do";//——确定调用函数
	ListView list;//——信件一个list
	int scrollState;
	int count = 40;
	int lastItem;
	int visibleItemCount;
	//Button footerButton;
	//LinearLayout footerProgressBarLayout;// 加载进度滚动条
	ListAdapter listAdapter = new ListAdapter();
	//View viewfoot;
	private User user=null;//——一个用户
	private int uid;
	private List<CLHotPost> hotPostList = new ArrayList<CLHotPost>();//——新建一个叫circle类型的数组
	private Context context;
	private ListView listView = null;//——一个界面显示
	//——这个是函数
	Handler myHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MyHotListPage.OK:
				if (null != hotPostList) {
					listAdapter.count = hotPostList.size();
				} else {
					listAdapter.count = 0;
				}
				listAdapter.notifyDataSetChanged();
				break;
			}
			super.handleMessage(msg);
		}
	};

	public MyHotListPage(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public MyHotListPage(Context context, User user) {
		super(context);
		this.user = user;
		this.context = context;
		//uid=user.getId();
		uid=124;
		init();
	}

	private void init() {
		setBackgroundColor(Color.parseColor("#ecf6eb"));
		listAdapter.count=0;
		listView = new ListView(getContext());
		listView.setDivider(new ColorDrawable(Color.parseColor("#ecf6eb")));
		listView.setDividerHeight(1);
		listView.setVerticalFadingEdgeEnabled(false);
		listView.setAdapter(listAdapter);
		listView.setOnScrollListener(this);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				CLHotPost onSelect = hotPostList.get(arg2);
				if(arg2==0)
				{
					Intent it = new Intent(context,MainStudyActivity.class);
					it.putExtra("user", user);
					it.putExtra("onSelect", onSelect);
					context.startActivity(it);
				}
				if(arg2==1)
				{
					
					Intent it = new Intent(context,DocListActivity.class);
					it.putExtra("user", user);
					it.putExtra("onSelect", onSelect);
					context.startActivity(it);
				}
				
			}
		});
		
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		addView(listView, params);
		Runnable run = new myThread();
		new Thread(run).start();
	}
	
	private class myThread implements Runnable {
		@Override
		public void run() {
			Message message = new Message();
			System.out.println("进入函数");
			hotPostList = new ActionUtils().myHot(URL_CIRCLELIST, uid);
			message.what = OK;
			myHandler.sendMessage(message);
		}
	}

	class ListAdapter extends BaseAdapter {

		int count = 10;

		@Override
		public int getCount() {
			return count;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			View view = inflater.inflate(R.layout.listview_item_hot, null);
			
			LinearLayout aa = (LinearLayout)view.findViewById(R.id.main_linear_layout);
			/*
			TextView tv = (TextView) view
					.findViewById(R.id.content);
			tv.setText(hotPostList.get(position).getHotName());
			tv = (TextView) view.findViewById(R.id.send_time);
			tv.setText(hotPostList.get(position).getPostData());
			
			tv = (TextView) view.findViewById(R.id.txt_wb_item_uname);
			tv.setText("" + hotPostList.get(position).getHotChannel());
			
			tv = (TextView) view.findViewById(R.id.txt_wb_item_from);
			tv.setText("" + hotPostList.get(position).getPostData());
			*/
			if(hotPostList.get(position).getHotType()==0)
			{
				aa.setBackgroundResource(R.drawable.bg_green);
				TextView tv = (TextView) view
						.findViewById(R.id.content);
				tv.setText("资料共享区");
			}
			
			return view;
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		this.visibleItemCount = visibleItemCount;
		lastItem = firstVisibleItem + visibleItemCount - 1;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		this.scrollState = scrollState;
	}
}
